/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.ws;

import javax.jws.WebService;
import aic.beans.*;
import aic.faults.*;
import aic.lists.CustomerList;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebService(portName = "NotificationPT", endpointInterface = "aic.ws.SMSService", serviceName = "NotificationService")
public class SMSServiceImpl implements SMSService {

    private final int MAXLENGTH = 160;

    public Boolean notify_customer(Customer customer, String message) {
        if (message.length() > MAXLENGTH) {
            throw MessageTooLongFaultFactory.createSOAPMessageTooLongFault("http://bittschen.net/SMSService/notify_customer", MAXLENGTH, message.length(), message);
        }

        // IF CUSTOMER DOESN'T EXIST
        if (!CustomerList.contains(customer)) {
            UnknownCustomerFault ucf = UnknownCustomerFaultFactory.createSOAPUnknownCustomerFault("http://bittschen.net/OrderManagementService/place_order", customer);
            throw ucf;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");

        System.out.println("[SMSService] " + new Date() + " :: " + customer.getName());
        System.out.println("[SMSService] Sending message :: " + message);
        System.out.print("[SMSService] Success :: ");

        if (Math.random() < Double.parseDouble("0.5")) {
            System.out.println("true");
            return true;
        } else {
            System.out.println("false");
            return false;
        }
    }
}

